package top.suven.base.http.admin.sec.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.suven.base.core.mybatis.PageData;
import top.suven.base.http.admin.sec.dao.MenuDao;
import top.suven.base.http.admin.sec.model.Menu;
import top.suven.base.http.admin.sec.service.MenuService;
import top.suven.base.http.admin.sec.vo.response.MenuResponseVo;
import top.suven.base.http.enums.EnableStatusEnum;
import top.suven.base.http.enums.StatusEnum;
import top.suven.base.util.utilize.BeanUtil;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Title: MenuService.java
 * @Description: 菜单的业务处理服务类
 * @author lixiangling
 * @date   2018/08/13 16:58:49
 * @version V1.0.0
 * ----------------------------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *
 * ----------------------------------------------------------------------------
 *
 */
@Component("menuService")
public class MenuServiceImpl implements MenuService {
    private final static Logger logger = LoggerFactory.getLogger(MenuServiceImpl.class);
    @Autowired
    private MenuDao menuDao;
    /**
     * @Title: 获取菜单分页信息
     * @Description:
     * @param menu @{Link Menu}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 16:58:49
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    public PageData<MenuResponseVo> getMenuPage(Menu menu, int start, int pageSize){
        int count = menuDao.countEntity(menu);
        if(count == 0){
            return new PageData<MenuResponseVo>(Collections.EMPTY_LIST,count);
        }
        List<Menu> menuList = menuDao.findPageEntity(menu, start, pageSize);
        if(menuList == null || menuList.isEmpty()){
            return new PageData<MenuResponseVo>(Collections.EMPTY_LIST,count);
        }
        List<MenuResponseVo> menuResponseList = menuList.stream().map(menuResponse -> {
            MenuResponseVo menuResponseVo = new MenuResponseVo();
            BeanUtil.copyProperties(menuResponse, menuResponseVo);
            return menuResponseVo;
        }).collect(Collectors.toList());
        return new PageData<MenuResponseVo>(menuResponseList,count);
    }
    /**
     * @Title: 获取菜单列表信息
     * @Description:
     * @param menu @{Link Menu}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 16:58:49
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    public List<Menu> getMenuList(Menu menu){
        return menuDao.findEntity(menu);
    }

    @Override
    public int getMenuCountByParentMenuCode(String parentMenuCode) {
        Menu menu = new Menu();
        menu.setParentMenuCode(parentMenuCode);
        menu.setStatus(StatusEnum.VALID.getIndex());
        menu.setEnableStatus(EnableStatusEnum.ENABLE.getIndex());
        return menuDao.countEntity(menu);
    }

    /**
     * @Title: 新增菜单信息
     * @Description:
     * @param menu @{Link Menu}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 16:58:49
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    public void addMenu(Menu menu){
        menuDao.createEntity(menu);
    }
    /**
    * @Title: 修改菜单信息
    * @Description:
    * @param menu @{Link Menu}
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 16:58:49
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    public void updMenu(Menu menu){
        menuDao.updateEntity(menu);
    }
   /**
    * @Title: 获取菜单信息
    * @Description:
    * @param id @{Link String}
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 16:58:49
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    public Menu getMenu(String id){
        Menu menu = new Menu();
        menu.setMenuCode(id);
        return menuDao.retrieveEntity(menu);
    }
    /**
     * @Title: 删除菜单信息
     * @Description:
     * @param ids @{Link String[]}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 16:58:49
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    public void del(String[] ids){
        menuDao.del(Arrays.asList(ids));
    }
   /**
    * @Title: 启用菜单信息
    * @Description:
    * @param ids @{Link String[]}
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 16:58:49
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    public void turnOn(String[] ids){
        menuDao.updateEnableStatus(EnableStatusEnum.ENABLE.getIndexStr(), Arrays.asList(ids));
    }
   /**
    * @Title: 禁用菜单信息
    * @Description:
    * @param ids @{Link String[]}
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 16:58:49
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    public void turnOff(String[] ids){
        menuDao.updateEnableStatus(EnableStatusEnum.DISABLE.getIndexStr(), Arrays.asList(ids));
    }
}